type vec3 = 
{
  x : float;
  y : float;
  z : float;
};;

let sub v1 v2 = 
  { x = v1.x -. v2.x;
    y = v1.y -. v2.y;
    z = v1.z -. v2.z; };;

let add v1 v2 = 
  { x = v1.x +. v2.x;
    y = v1.y +. v2.y;
    z = v1.z +. v2.z; };;

let scale v d =
  { x = v.x *. d;
    y = v.y *. d;
    z = v.z *. d; };;

let midpoint v1 v2 = 
  add (scale (sub v2 v1) 0.5) v1;;

let len v =
  sqrt (v.x *. v.x) +. (v.y *. v.y) +. (v.z *. v.z);;

let lengthize v d =
  let mult = (len v) /. d in
    scale v mult;;

let cross a b =
  { x = (a.y *. b.z -. a.z *. b.y);
    y = (a.x *. b.z -. a.z *. b.x);
    z = (a.x *. b.y -. a.y *. b.x); } ;;

let dot a b = 
  a.x *. b.x +. a.y *. b.y +. a.z *. b.z;;


let printVec v =
  Printf.printf "%f %f %f\n" v.x v.y v.z;;
